# ---------------------------------------------
# Makefile for AOD System (Phase 5: Professional Build)
# ---------------------------------------------

# 1. 定义编译器和标准
CXX := g++
CXXFLAGS := -std=c++14 -Wall -g

# 2. (核心) 使用 mysql_config 自动获取编译和链接标志
# $(shell ...) 是 Makefile 的函数，用于执行 shell 命令
# --cflags 会输出: -I/usr/include/mysql
# --libs 会输出:   -lmariadb
MYSQL_CFLAGS := $(shell mysql_config --cflags)
MYSQL_LIBS   := $(shell mysql_config --libs)
JSONCPP_LIBS := -ljsoncpp

# 3. 定义源文件
# 3.1 共享的代码
AOD_SOURCES := src/data.cpp

# 3.2 可执行目标编译
TARGET_SERVER := aod_server
TARGET_DBTEST := db_test
TARGET_UTILTEST := util_test 
SRC_SERVER := src/test/main.cpp
SRC_DBTEST := src/test/db_test.cpp
SRC_UTILTEST := src/test/util_test.cpp 

# 4. 默认目标 'all'
all: $(TARGET_SERVER) $(TARGET_DBTEST) $(TARGET_UTILTEST)

# 5. 构建 "Hello, Server" (aod_server)
$(TARGET_SERVER): $(SRC_SERVER) $(AOD_SOURCES)
	$(CXX) $(CXXFLAGS) $(MYSQL_CFLAGS) -o $(TARGET_SERVER) $(SRC_SERVER) $(AOD_SOURCES) -lpthread $(MYSQL_LIBS) $(JSONCPP_LIBS)

# 6. 构建 "Hello, Database" (db_test)
#    注意我们是如何使用 $(MYSQL_CFLAGS) 和 $(MYSQL_LIBS) 的
$(TARGET_DBTEST): $(SRC_DBTEST) $(AOD_SOURCES)
	$(CXX) $(CXXFLAGS) $(MYSQL_CFLAGS) -o $(TARGET_DBTEST) $(SRC_DBTEST) $(AOD_SOURCES) $(MYSQL_LIBS) $(JSONCPP_LIBS)

# 7. 构建 "Utility Test" (util_test)
#    它只需要：自己的 main + jsoncpp
$(TARGET_UTILTEST): $(SRC_UTILTEST)
	$(CXX) $(CXXFLAGS) -o $(TARGET_UTILTEST) $(SRC_UTILTEST) $(JSONCPP_LIBS) # <-- 编译 util_test

# 8. “伪目标”：clean
clean:
	rm -f $(TARGET_SERVER) $(TARGET_DBTEST) $(TARGET_UTILTEST) 